/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Enum.LoaiNguoiDungEnum;
import java.lang.reflect.Field;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.NguoiDung;
import util.NewHibernateUtil;

/**
 *
 * @author 0
 */
public class BaseDAO {
    public static List<?> LayDanhSach(Class<?> clazz,NguoiDung nguoiDung) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string=String.format("from %s o where (o.isDelete=false or o.isDelete=null)",clazz.getName());
        if(nguoiDung.getLoaiNguoiDung().getId()!=LoaiNguoiDungEnum.Admin.getCode() && existCuaHang(clazz.getDeclaredFields())){
            string+=String.format(" and o.cuaHang.id=%d",nguoiDung.getCuaHang().getId());
        }
        List<?> list=session.createQuery(string).list();
        session.close();
        return list;
    }
    
    public static Object LayTheoID(Class<?> clazz,Integer id,NguoiDung nguoiDung) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string=String.format("from %s o where (o.isDelete=false or o.isDelete=null) and o.id=%d",clazz.getName(),id);
        if(nguoiDung.getLoaiNguoiDung().getId()!=LoaiNguoiDungEnum.Admin.getCode() && existCuaHang(clazz.getDeclaredFields())){
            string+=String.format(" and o.cuaHang.id=%d",nguoiDung.getCuaHang().getId());
        }
        Object object=session.createQuery(string).uniqueResult();
        session.close();
        return object;
    }
    
    protected static boolean existCuaHang(Field[] fields){
        for(Field f:fields)
        {
            if(f.getName().contains("cuaHang")){
                return true;
            }
        }
        return false;
    }
    
    public static int Them(Object object) throws Exception{
        int id=0;
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            id=(int) session.save(object);
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
        return id;
    }
    
    public static void Xoa(List<?> needDeleteList) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            for(Object object:needDeleteList){
                session.update(object);
            }
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
    }
    
    //Không nên dùng
    public static void HardDelete(List<?> needDeleteList) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            for(Object object:needDeleteList){
                session.delete(object);
            }
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
    }
    
    public static void Sua(Object object) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            session.update(object);
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
    }
    
    public static Object LayTheoID(Class<?> clazz,Integer id) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string=String.format("from %s o where (o.isDelete=false or o.isDelete=null) and o.id=%d",clazz.getName(),id);
        Object object=session.createQuery(string).uniqueResult();
        session.close();
        return object;
    }
}
